package com.example.demo.controller;

import com.example.demo.common.BusinessException;
import com.example.demo.common.ErrorEnum;
import com.example.demo.config.myannotation.code.LogAnno.LogAnno;
import com.example.demo.entity.Company;
import com.example.demo.outstyle.Message;
import com.example.demo.outstyle.TableData;
import com.example.demo.service.CompanyService;
import com.example.demo.util.UUidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api(tags = "company")
@Controller
@RequestMapping(value = "/logic/company")
@CrossOrigin
@ResponseBody
public class CompanyController {
    @Autowired
    private CompanyService companyServiceImp;


    @ApiOperation(value = "插入公司信息")
    @PostMapping("/addCompanyInfo")
    @LogAnno(operatType = "插入公司信息")
    public Message addCompany(Company company) {
        company.setId(UUidUtil.getUUid());
        int check = companyServiceImp.addCompany(company);
        Message msg = new Message();
        if(check != 0){
            msg.setMsg("成功创建公司");
            msg.setState(0);
        } else{
            throw new BusinessException(ErrorEnum.E_charushibai);
        }
        msg.setData(company);
        return msg;
    }

    @ApiOperation(value = "分页查询公司信息")
    @PostMapping("/select")
    @LogAnno(operatType = "分页查询公司信息")
    public TableData<Company> searchCompany(int page,int limit,Company example) {
        TableData<Company> result = new TableData();

        List<Company> userList = companyServiceImp.selectAll(page, limit,example);
        int allCount = companyServiceImp.selectCounts();

        result.setState(0);
        result.setCount(allCount);
        result.setData(userList);
        result.setMsg("成功加载");

        return result;
    }

    @ApiOperation(value = "删除公司信息")
    @PostMapping("/deleteCompanyInfo")
    @LogAnno(operatType = "删除公司信息")
    public Message deleteCompany(String id) {
        int result = companyServiceImp.deleteCompanyById(id);
        Message msg = new Message();
        if(result > 0){
            msg.setMsg("成功删除");
            msg.setState(0);
        } else{
            throw new BusinessException(ErrorEnum.E_shanchushibai);
        }
        msg.setData(result);
        return msg;
    }

    @ApiOperation(value = "更改公司信息")
    @PostMapping("/updateCompanyInfo")
    @LogAnno(operatType = "更改公司信息")
    public Message modifyCompany(Company user) {
        int result = companyServiceImp.updateCompany(user);

        Message msg = new Message();
        if(result > 0){
            msg.setMsg("成功修改");
            msg.setState(0);
        } else{
            throw new BusinessException(ErrorEnum.E_xiugaishibai);
        }
        msg.setData(result);
        return msg;
    }
}
